package com.jiaxu.fileexplorer.activities;

import android.content.DialogInterface;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jiaxu.fileexplorer.R;
import com.jiaxu.fileexplorer.adapter.Myadapter;
import com.jiaxu.fileexplorer.utils.Globals;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<Map<String,Object>> values;
    private Myadapter myadapter;
    private TextView nowPath;



   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Globals.init(this);
       listView = (ListView) findViewById(R.id.list);
       nowPath = (TextView) findViewById(R.id.now_path);

       values = new ArrayList<>();
       final File sd = Environment.getExternalStorageDirectory();
       loadFile(sd);
       myadapter = new Myadapter(this,values);
       listView.setAdapter(myadapter);
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               Map<String,Object> map = values.get(i);
               File file = new File(map.get("fullpath").toString());
                if((Boolean) map.get("isdir")){
                    values.clear();
                    if(!sd.getAbsolutePath().equals(file.getAbsolutePath())){
                        Map<String,Object> parentPath = new HashMap<>();
                        parentPath.put("extname","open_dir");
                        parentPath.put("filename","返回上一级");
                        parentPath.put("isdir",true);
                        parentPath.put("fullpath",file.getParent());
                        parentPath.put("isreal","TRUE");
                        values.add(parentPath);
                    }
                    loadFile(file);
                    myadapter.notifyDataSetChanged();
                }else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("文件信息");
                    builder.setMessage("文件大小为："+file.length()+"\r\n"+"文件名："+file.getName());
                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.create().show();
                }
           }
       });

       listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
               final Map<String,Object> map = values.get(i);
               boolean isDir = (boolean) map.get("isdir");
               if(!isDir){
                   AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                   builder.setTitle("注意");
                   builder.setMessage("您确认要删除该文件吗？");
                   builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialogInterface, int i) {
                           File file = new File((String) map.get("fullpath"));
                           file.delete();
                           values.remove(i);
                           myadapter.notifyDataSetChanged();
                       }
                   });
                   builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialogInterface, int i) {

                       }
                   });
                   builder.create().show();
               }
               return false;
           }
       });

   }
    private void loadFile(File path){
        File[] fs = path.listFiles();
        nowPath.setText("当前路径："+path.getAbsolutePath());
        if(fs!=null){
            for(File i:fs){
                Map<String,Object> map = new HashMap<>();
                map.put("filename",i.getName());
                map.put("fullpath",i.getAbsolutePath());
                if(i.isDirectory()){
                    map.put("extname","close_dir");
                    map.put("isdir",true);
                }else {
                    map.put("extname",i.getName().substring(i.getName().lastIndexOf(".")+1).toLowerCase());
                    map.put("isdir",false);
                }
                values.add(map);
            }
        }else {
            nowPath.setText("no file!");
        }
    }
//重定义返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            Map<String,Object> map = values.get(0);
            if("TRUE".equals(map.get("isreal"))){
                listView.performItemClick(listView.getChildAt(0),0,listView.getChildAt(0).getId());
            }else {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("注意");
                builder.setTitle("确定要退出吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.create().show();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
